package org.southyin.demo01;

import org.southyin.utils.DBUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;


public class JDBCDemo01 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        try {
            connection = DBUtils.getInstance().getConnection();
            // 3.通过连接，获取执行的SQL对象
            String sql = "select id,ename,job,birthday from emp";
            stat = connection.prepareStatement(sql);
            // 4.执行SQL，获取结果
            rs = stat.executeQuery();
            // 5.遍历
            while (rs.next()) {
                long id = rs.getLong("id");
                String ename = rs.getString("ename");
                String job = rs.getString("job");
                Date birthday = new Date(rs.getDate("birthday").getTime());
                System.out.println(id + "," + ename + "," + job + "," + birthday);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 6.关闭连接
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(stat);
            DBUtils.getInstance().close(connection);
        }
    }
}
